#ifndef COctreeManager_H
#define COctreeManager_H
#include <osgGe/BoundingBox>


#include <iostream>
#include <vector>

class COctree;
class COctreeSceneManager;

//这个是节点数据的基类
class  COctreeNode
{
public:
	COctreeNode();
	virtual osg::BoundingBoxd getWorldAABB();

	virtual bool isInteractor(COctreeNode* node);

	COctree *getOctant();

	void setOctant(COctree *o);
	bool _isIn(osg::BoundingBoxd &box);

public:

	COctree *mOctant;

	bool mIsMat;

	osg::BoundingBoxd mWorldAABB;

};

class  InteractorNode : public COctreeNode
{
public:
	InteractorNode();

	virtual osg::BoundingBoxd getWorldAABB();

//	std::vector<OcDbObjectId> mids;
};

class  COctree
{
public:
	COctree(void);
	COctree(COctree * parent);
	~COctree(void);
	void _addNode(COctreeNode * n);
	bool _isTwiceSize( osg::BoundingBoxd &box);
	void _getChildIndexes(const osg::BoundingBoxd &box, int *x, int *y, int *z) const;
	void _removeNode(COctreeNode * n);
	void _getCullBounds(osg::BoundingBoxd *b) const;

	inline void _ref()
	{
		mNumNodes++;

		if (mParent != 0) mParent->_ref();
	};

	/** Decrements the overall node count of this octree and all its parents
	*/
	inline void _unref()
	{
		mNumNodes--;

		if (mParent != 0) mParent->_unref();
	};

public:
	osg::BoundingBoxd mBox;
	COctree * mChildren[2][2][2];

	std::list<COctreeNode*> mNodes;

	//在当前COctree及其子树中的所有COctreeNode的数目
	int mNumNodes;
	COctree * mParent;

	osg::Vec3 mHalfSize;
};

enum EGRCameraType
{
	EGRCT_Default = 150,     //
	EGRCT_BSP = 151,     //
	EGRCT_OctTree = 152,     //
	EGRCT_PSZ = 153,     //
};
typedef unsigned long GRCameraType;

class  COctreeSceneManager
{
public:
	COctreeSceneManager(void);
	virtual ~COctreeSceneManager(void);

public:
	virtual GRCameraType GetCameraType(void);

public:
	void _updateOctreeNode(COctreeNode * onode);
	void _addOctreeNode(COctreeNode * n, COctree *octant, int depth);
	void _removeOctreeNode(COctreeNode * n);
	void init(osg::BoundingBoxd &box, int depth);
	static void findNodes(COctree *octant, COctreeNode *srcNode, COctreeNode *exclude, std::vector<COctreeNode*>& desNodes);


public:

	COctree *mOctree;
	int mMaxDepth;

	int mNumObjects;

	//整个OCtree的大小
	osg::BoundingBoxd mBox;

	std::vector<COctreeNode*> mTotalNodes;


};

#endif


